package com.hollycrm.hollysqm.core.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hollycrm.hollysqm.core.index.dao.IndexSearchDao;
import com.hollycrm.hollysqm.core.index.factory.IndexSearchFactory;
import com.hollycrm.hollysqm.core.index.service.IndexSearchService;
import com.hollycrm.hollysqm.core.vo.I8DocBean;
import com.hollycrm.hollysqm.core.vo.V8DocBean;
import com.hollycrm.hollysqm.util.Constant;

@Controller
public class WebTestAction {

	@Value("${zkHost}")
	private String zkHost ;
	/**
     * 获取solrCloud集合
     */
    @Value("${solr.collection}")
    private String defaultCollection;
	
//	@Autowired
	private IndexSearchDao indexSearchDaoImpl;
	
	@Autowired
	private IndexSearchFactory indexSearchFactory;

	@RequestMapping(value="/test",method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String index(Map<String, Object> model) {		
		System.out.println(zkHost+"---------------------"+defaultCollection);
		query();
		return zkHost;
	}	
	
	public void query(){
	//	IndexSearchDaoImpl indexSearchDao = (IndexSearchDaoImpl)context.getBean("indexSearchDaoImpl");		
		HashMap<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("day", "[20170301 TO 20170331]");
		String qs = "*:*";//"txtContent:(*电话号码*)";
		String tag = Constant.V8;
		IndexSearchService indexSearchService = indexSearchFactory.getNewIndexService(tag);//根据V8或I8标识返回索引对象
		try{
//			System.out.println(indexSearchDaoImpl);
			System.out.println("开始查询");
//			List<V8DocBean> list= indexSearchDaoImpl.queryResult(qs, paramMap, V8DocBean.class);
			
			if (tag.equalsIgnoreCase(Constant.V8)){//切换成V8
				List<V8DocBean> list= indexSearchService.queryResult(qs, paramMap, V8DocBean.class);
				V8DocBean bean = list.get(0);
//				indexSearchService.updateDoc(bean);
				System.out.println("共查询："+(list!=null?list.size():0));
				System.out.println(bean.getId() + " \t TxtContent:"+bean.getTxtContent());
			}else if (tag.equalsIgnoreCase(Constant.I8)){//切换成I8
				List<I8DocBean> list= indexSearchService.queryResult(qs, paramMap, I8DocBean.class);
				I8DocBean bean = list.get(0);
				System.out.println("共查询："+(list!=null?list.size():0));
				System.out.println(bean.getId() + " \t TxtContent:"+bean.getTxtContent());
			}			
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println("--------query-------");
	}
	
	
	@RequestMapping(value="/hQuery",method = RequestMethod.GET)
	@ResponseBody
	public String highlightQuery(){
		HashMap<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("day", "[20170301 TO 20170301]");
		String qs = "txtContent:(*电话号码*)";
		String tag = Constant.V8;
		IndexSearchService indexSearchService = indexSearchFactory.getNewIndexService(tag);//根据V8或I8标识返回索引对象
		try{			
			List<V8DocBean> list= indexSearchService.queryResult(qs, paramMap, true, V8DocBean.class);
			for (V8DocBean bean : list){
				System.out.println(bean.getId()+"\t"+bean.getTxtContent());
			}						
		}catch(Exception e){
			e.printStackTrace();
		}		
		return "ok_"+System.currentTimeMillis();
	}
	
	
}
